import java.util.Random;

public class Group 
{
	private Team8_Player[] groupFirstPlayer;
	private Team8_Player[] groupSecondPlayer;
	
	private int[] scores;
	
	private int numPlayers;
	private int numWeights;
	
	public Group(int numPlayers, int numWeights)
	{
		this.numPlayers = numPlayers;
		this.numWeights = numWeights;
		
		groupFirstPlayer = new Team8_Player[numPlayers];
		groupSecondPlayer = new Team8_Player[numPlayers];
		
		
		
		scores = new int[numPlayers];
		
	}
	
	public void createGroupWithKnownWeights(WeightCollection[] weightCollections)
	{
		for(int i = 0; i < numPlayers; i++)
		{
			Team8_Player first = new Team8_Player(0, weightCollections[i]);
			Team8_Player second = new Team8_Player(1, weightCollections[i]);
			
			groupFirstPlayer[i] = first;
			groupSecondPlayer[i] = second;
		}
	}
	
	
	public void createGroup()
	{
		Random r = new Random();
		for(int i = 0; i < numPlayers; i++)
		{
			int[] temp = new int[numWeights];
			for(int j = 0; j < numWeights; j++)
			{
				// Currently index 3 is for MaxAmountLoseToCapture which should have a negative weight for the heuristic 
				if(j == 3)
					temp[j] = (r.nextInt(30)+1)*-1;
				else
					temp[j] = r.nextInt(30)+1;
			}
			
			WeightCollection WC = new WeightCollection(temp);
			
			Team8_Player first = new Team8_Player(0, WC);
			Team8_Player second = new Team8_Player(1, WC);
			
			groupFirstPlayer[i] = first;
			groupSecondPlayer[i] = second;
		}
	}
	
	public void fightGroup() throws Exception
	{
		//Fight players as first player
		for(int i = 0; i < numPlayers; i++)
		{
			for(int j = 0; j < numPlayers; j++)
			{
				if(i != j)
				{
					if(play(groupFirstPlayer[i], groupSecondPlayer[j]))
					{
						scores[i] = scores[i] + 1;
					}
				}
			}
		}
		
		//Fight players as second player
		for(int i = 0; i < numPlayers; i++)
		{
			for(int j = 0; j < numPlayers; j++)
			{
				if(i != j)
				{
					if(!play(groupFirstPlayer[j], groupSecondPlayer[i]))
					{
						scores[i] = scores[i] + 1;
					}
				}
			}
		}
		
	}
	
	private boolean play(Team8_Player p1, Team8_Player p2) throws Exception
	{
		//initialize the variables
		Team8_Player player[] = new Team8_Player[2];
		player[0] = p1;
		player[1] = p2;
		MancalaGameState gs = new KalahGameState(4);
		
		//play out the game- not completely sure if this works, i just pasted the play function in termgame
		while (!gs.checkEndGame()) {
	            int move = player[gs.CurrentPlayer()].getMove(gs);
	            while (!gs.validMove(move)) {
	                move = player[gs.CurrentPlayer()].getMove(gs);
	            }
	            gs.play(move);
	    }
		
		//Award winning player the points
	    gs.computeFinalScore();
	    
	    //Returns true if player 1 wins, and false if player 2 wins
	    return (gs.getScore(0) > gs.getScore(1));
	}
	
	public WeightCollection getWinnersWeights()
	{
		int winnersScore = Integer.MIN_VALUE;
		int winnersIndex = 0;
		
		for(int i = 0; i < scores.length; i++)
		{
			if(scores[i] > winnersScore)
			{
				winnersScore = scores[i];
				winnersIndex = i;
			}
		}
		
		return groupFirstPlayer[winnersIndex].getWeights();
	}
	
}
